home *** CD-ROM | disk | FTP | other *** search
/ Macworld Expo - Develope…Central & Net Innovations / Developer Central and Net Innovators (MacWorld Expo) (January 1999).iso / Developer Central / Bowers Development / Demo AppMaker / Examples / C⁄C++ OS8 / Gadgets / Buttons.cp < prev    next >
Encoding:
Text File  |  1998-09-06  |  7.9 KB  |  393 lines  |  [TEXT/CWIE]

  1. // Buttons.cp
  2.  
  3. #include <Types.h>
  4. #include <Quickdraw.h>
  5. #include <Controls.h>
  6. #include <Dialogs.h>
  7. #include <Events.h>
  8. #include <Lists.h>
  9. #include <Menus.h>
  10. #include <Resources.h>
  11. #include <Sound.h>
  12. #include <TextEdit.h>
  13. #include <ToolUtils.h>
  14. #include <Appearance.h>
  15.  
  16. #include "Globals.h"
  17. #include "ResourceDefs.h"
  18. #include "DoScrap.h"
  19. #include "Miscellany.h"
  20. #include "Scrolling.h"
  21. #include "ControlUtils.h"
  22. #include "DDocData.h"
  23. #include "GadgetsEngine.h"
  24. #include "GadgetsDoc.h"
  25.  
  26. #include "Buttons.h"
  27.  
  28.  
  29. //----------
  30. void    Buttons::Create (
  31.     AMDoc*            inDoc,
  32.     DDocData*        inData)
  33. {
  34.     Buttons*        winObj = new Buttons;
  35.  
  36.     if (winObj != nil) {
  37.         winObj->Open (inDoc);
  38.         winObj->ConnectToData (inData);
  39.     }
  40. }
  41.  
  42. //----------
  43. Buttons::Buttons ()
  44. {
  45.     mData = nil;
  46. }
  47.  
  48. //----------
  49. Buttons::~Buttons ()
  50. {
  51. }
  52.  
  53. //----------
  54. GadgetsEngine*    Buttons::GetEngine ()
  55. {
  56.     return (GadgetsEngine*) mDoc->mEngine;
  57. }
  58.  
  59. //----------
  60. void    Buttons::Open (
  61.     AMDoc*            inDoc)
  62. {
  63.     WindowPtr        window;
  64.     Handle            wftb;
  65.  
  66.     mDoc = inDoc;
  67.  
  68.     window = GetNewCWindow (WIND_Buttons, nil, (WindowPtr) -1L);
  69.     if (mDoc->mEngine->GetFilename () [0] != 0) {
  70.         SetWTitle (window, mDoc->mEngine->GetFilename ());
  71.     }
  72.     mWindow = window;
  73.     ((GadgetsDoc*)mDoc)->mButtonsPtr = window;
  74.  
  75.     SetWindowKind (window, 'AM');
  76.     SetWRefCon (window, (long) this);
  77.     SetPort (window);
  78.     SetInfo (window);
  79.  
  80.     wftb = ::GetResource ('Wftb', WIND_Buttons);
  81.  
  82.     CreateRootControl (window, &mRootControl);
  83.  
  84.     vScroll = nil;
  85.     hScroll = nil;
  86.  
  87.  
  88.     mStandardHandle = ::GetNewControl (CNTL_Standard, window);
  89.     SetWindowItemFont (mStandardHandle, wftb, 1);
  90.     SetDefaultState (mStandardHandle, true);
  91.  
  92.     mRightIconHandle = ::GetNewControl (CNTL_RightIcon, window);
  93.     SetWindowItemFont (mRightIconHandle, wftb, 2);
  94.  
  95.     mBevelHandle = ::GetNewControl (CNTL_Bevel, window);
  96.     SetWindowItemFont (mBevelHandle, wftb, 3);
  97.     SetBevelButtonTextPlacement (mBevelHandle, kControlBevelButtonPlaceToRightOfGraphic);
  98.     SetBevelButtonTextAlignment (mBevelHandle, kControlBevelButtonAlignTextFlushLeft, 0);
  99.     SetBevelButtonGraphicAlignment (mBevelHandle, kControlBevelButtonAlignLeft, 0, 0);
  100.  
  101.     mXxHandle = ::GetNewControl (CNTL_Xx, window);
  102.     SetWindowItemFont (mXxHandle, wftb, 4);
  103.  
  104.     mCapTriangleLabel = GetNewControl (CNTL_CapTriangle, window);
  105.     SetWindowItemFont (mCapTriangleLabel, wftb, 5);
  106.     SetControlFromTEXT (mCapTriangleLabel, TEXT_CapTriangle);
  107.  
  108.     mLightHandle = ::GetNewControl (CNTL_Light, window);
  109.     SetWindowItemFont (mLightHandle, wftb, 6);
  110.  
  111.     mLeftRightHandle = ::GetNewControl (CNTL_LeftRight, window);
  112.     SetWindowItemFont (mLeftRightHandle, wftb, 7);
  113.  
  114.     mRadiosBoxHandle = GetNewControl (CNTL_RadiosBox, window);
  115.     SetWindowItemFont (mRadiosBoxHandle, wftb, 8);
  116.     mRadiosGroupHandle = GetNewControl (CNTL_RadiosGroup, window);
  117.     EmbedControl (mRadiosGroupHandle, mRadiosBoxHandle);
  118.  
  119.     mRadioButtonHandle = ::GetNewControl (CNTL_RadioButton, window);
  120.     EmbedControl (mRadioButtonHandle, mRadiosGroupHandle);
  121.     SetWindowItemFont (mRadioButtonHandle, wftb, 10);
  122.     SetBevelButtonGraphicAlignment (mRadioButtonHandle, kControlBevelButtonAlignCenter, 0, 0);
  123.  
  124.     mRadioButton2Handle = ::GetNewControl (CNTL_RadioButton2, window);
  125.     EmbedControl (mRadioButton2Handle, mRadiosGroupHandle);
  126.     SetWindowItemFont (mRadioButton2Handle, wftb, 11);
  127.     SetBevelButtonGraphicAlignment (mRadioButton2Handle, kControlBevelButtonAlignCenter, 0, 0);
  128.  
  129.     mInvisibleHandle = ::GetNewControl (CNTL_Invisible, window);
  130.     SetWindowItemFont (mInvisibleHandle, wftb, 12);
  131.  
  132.     mCapInvisibleLabel = GetNewControl (CNTL_CapInvisible, window);
  133.     SetWindowItemFont (mCapInvisibleLabel, wftb, 13);
  134.     SetControlFromTEXT (mCapInvisibleLabel, TEXT_CapInvisible);
  135.  
  136.     AdvanceKeyboardFocus (window);
  137.  
  138.     ShowWindow (window);
  139. }
  140.  
  141. //----------
  142. void    Buttons::Close ()
  143. {
  144.     mData->RemoveResponder (this);
  145.  
  146.     ((GadgetsDoc*)mDoc)->mButtonsPtr = nil;
  147.     SetInfo (nil);
  148.     HideWindow (mWindow);
  149.     DisposeWindow (mWindow);
  150.  
  151.     delete this;
  152. }
  153.  
  154. //----------
  155. void    Buttons::ConnectToData (
  156.     DDocData*        inData)
  157. {
  158.     mData = inData;
  159.     mData->AddResponder (this);
  160.  
  161.     SetControlValue (mXxHandle, mData->GetTriangle ());
  162.     SetControlValue (mLeftRightHandle, mData->GetLeftRight ());
  163.     SetControlValue (mRadiosGroupHandle, mData->GetRadios ());
  164. }
  165.  
  166. //----------
  167. void    Buttons::DataChanged (
  168.     long        inDataID)
  169. {
  170.     if (inDataID == idTriangle) {
  171.         SetControlValue (mXxHandle, mData->GetTriangle ());
  172.     }
  173.     if (inDataID == idLeftRight) {
  174.         SetControlValue (mLeftRightHandle, mData->GetLeftRight ());
  175.     }
  176.     if (inDataID == idRadios) {
  177.         SetControlValue (mRadiosGroupHandle, mData->GetRadios ());
  178.     }
  179. }
  180.  
  181. //----------
  182. void    Buttons::Control (
  183.     ControlHandle        whichControl,
  184.     short                whichPart,
  185.     Point                where)
  186. {
  187.     Rect            bounds;
  188.     short            newValue;
  189.  
  190.     ExitCurField ();
  191.  
  192.     if (whichControl == mStandardHandle) {
  193.         if (TrackClick (mStandardHandle, where)) {
  194.         }
  195.     }
  196.     if (whichControl == mRightIconHandle) {
  197.         if (TrackClick (mRightIconHandle, where)) {
  198.         }
  199.     }
  200.     if (whichControl == mBevelHandle) {
  201.         if (TrackClick (mBevelHandle, where)) {
  202.         }
  203.     }
  204.     if (whichControl == mXxHandle) {
  205.         if (TrackCheckbox (mXxHandle, where)) {
  206.             mData->SetTriangle (GetControlValue (mXxHandle) != 0);
  207.         }
  208.     }
  209.     if (whichControl == mLightHandle) {
  210.         if (TrackClick (mLightHandle, where)) {
  211.         }
  212.     }
  213.     if (whichControl == mLeftRightHandle) {
  214.         if (TrackCheckbox (mLeftRightHandle, where)) {
  215.             mData->SetLeftRight (GetControlValue (mLeftRightHandle) != 0);
  216.         }
  217.     }
  218.     if (whichControl == mRadiosGroupHandle) {
  219.         if (TrackClick (whichControl, where) != 0) {
  220.             mData->SetRadios (GetControlValue (mRadiosGroupHandle));
  221.         }
  222.     }
  223.     if (whichControl == mInvisibleHandle) {
  224.         if (TrackClick (mInvisibleHandle, where)) {
  225.         }
  226.     }
  227. }
  228.  
  229. //----------
  230. void    Buttons::MouseIn (
  231.     Point        where,
  232.     short        modifiers)
  233. {
  234.     Rect        bounds;
  235.  
  236. }
  237.  
  238. //----------
  239. void    Buttons::ExitCurField ()
  240. {
  241.     ControlHandle    focus;
  242.  
  243.     GetKeyboardFocus (mWindow, &focus);
  244.  
  245.     if (focus == nil) {
  246.         // nothing to exit
  247.  
  248.     }
  249. }
  250.  
  251. //----------
  252. void    Buttons::TypeIn (
  253.     char        ch)
  254. {
  255.     ControlHandle    focus;
  256.     SInt16            keyCode;
  257.  
  258.     GetKeyboardFocus (mWindow, &focus);
  259.  
  260.     if ((ch == charEnter)
  261.     ||  (ch == charReturn)) {
  262.         ExitCurField ();
  263.         SimulateClick (mStandardHandle);
  264.     } else if (ch == charEsc) {
  265.     } else if (ch == charTab) {
  266.         DoTab ((curEvent.modifiers & optionKey) != 0);
  267.     } else if (focus != nil) {
  268.         keyCode = (SInt16)(curEvent.message & keyCodeMask);
  269.         HandleControlKey (focus, keyCode, ch, (SInt16)curEvent.modifiers);
  270.         mDoc->mEngine->SetDirty ();
  271.     } else {
  272.         SysBeep (1);
  273.     }
  274. }
  275.  
  276. //----------
  277. void    Buttons::Resize ()
  278. {
  279.     /* application-specific code to resize items in window */
  280. }
  281.  
  282. //----------
  283. void    Buttons::Scroll (
  284.     short        newValue,
  285.     short        oldValue)
  286. {
  287.     /* application-specific code to scroll window */
  288.     if (gWhichScroll == vScroll) {
  289.     } else {    // horizontal
  290.     }
  291. }
  292.  
  293. //----------
  294. void    Buttons::DoUndo ()
  295. {
  296. } // DoUndo
  297.  
  298. //----------
  299. void    Buttons::DoCut ()
  300. {
  301.     TEHandle        curTE = GetCurTE ();
  302.  
  303.     if (curTE != nil) {
  304.         TECut (curTE);
  305.         mDoc->mEngine->SetDirty ();
  306.         scrapDirty = true;
  307.     }
  308. } // DoCut
  309.  
  310. //----------
  311. void    Buttons::DoCopy ()
  312. {
  313.     TEHandle        curTE = GetCurTE ();
  314.  
  315.     if (curTE != nil) {
  316.         TECopy (curTE);
  317.         scrapDirty = true;
  318.     }
  319. } // DoCopy
  320.  
  321. //----------
  322. void    Buttons::DoPaste ()
  323. {
  324.     TEHandle        curTE = GetCurTE ();
  325.  
  326.     if (curTE != nil) {
  327.         TEPaste (curTE);
  328.         mDoc->mEngine->SetDirty ();
  329.     }
  330. } // DoPaste
  331.  
  332. //----------
  333. void    Buttons::DoClear ()
  334. {
  335.     TEHandle        curTE = GetCurTE ();
  336.  
  337.     if (curTE != nil) {
  338.         TEDelete (curTE);
  339.         mDoc->mEngine->SetDirty ();
  340.     }
  341. } // DoClear
  342.  
  343. //----------
  344. void    Buttons::DoSelectAll ()
  345. {
  346.     TEHandle        curTE = GetCurTE ();
  347.  
  348.     if (curTE != nil) {
  349.         TESetSelect (0, 32767, curTE);
  350.     }
  351. } // DoSelectAll
  352.  
  353. //----------
  354. void    Buttons::DoShowClipboard ()
  355. {
  356. } // DoShowClipboard
  357.  
  358. //----------
  359. Boolean        Buttons::DoCommand (
  360.     long        inCommand)
  361. {
  362.     Boolean        result = true;
  363.  
  364.     switch (inCommand) {
  365.         case cmdUndo:
  366.                 DoUndo ();
  367.             break;
  368.         case cmdCut:
  369.                 DoCut ();
  370.             break;
  371.         case cmdCopy:
  372.                 DoCopy ();
  373.             break;
  374.         case cmdPaste:
  375.                 DoPaste ();
  376.             break;
  377.         case cmdClear:
  378.                 DoClear ();
  379.             break;
  380.         case cmdSelectAll:
  381.                 DoSelectAll ();
  382.             break;
  383.         case cmdShowClipboard:
  384.                 DoShowClipboard ();
  385.             break;
  386.  
  387.         default:
  388.                 result = false;
  389.     } // switch
  390.  
  391.     return result;
  392. }
  393.